package com.xgkj.diyiketang.activity.data;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.WitnessInfo;
import com.xgkj.diyiketang.data.WitnessListInfo;

public class JianzhengStatusItemAdapter extends BaseAdapter {

    private List<WitnessListInfo> mList = new ArrayList<>();

    private Context mContext;
    private LayoutInflater layoutInflater;

    public JianzhengStatusItemAdapter(Context context) {
        mContext = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void updateData(List<WitnessListInfo> list){
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.jianzheng_status_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        WitnessListInfo info = mList.get(position);
        initializeViews(info, (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(WitnessListInfo info, ViewHolder holder) {
        if (info != null){
            holder.cishu.setText(info.getWitCount());
            List<WitnessInfo> list = info.getList();
            int size = list.size();
            if (list != null && size > 0){
                holder.jianzhengStatusList.removeAllViews();
                for (int i=0;i<size;i++){
                    WitnessInfo witnessInfo = list.get(i);
                    if (witnessInfo != null) {
                        View view = layoutInflater.inflate(R.layout.yw_jianzhengstatus_item, holder.jianzhengStatusList, false);
                        ImageView statusImage = (ImageView) view.findViewById(R.id.status_image);
                        TextView nickText = (TextView) view.findViewById(R.id.nickname);
                        TextView statusText = (TextView) view.findViewById(R.id.status_txt);
                        TextView timeText = (TextView) view.findViewById(R.id.jianzheng_time);
                        String status = witnessInfo.getWitStatus();
                        String nick = witnessInfo.getWinNickName();
                        String time = witnessInfo.getCreateDate();
                        if (status != null && status.equals("0")){
                            statusImage.setImageResource(R.drawable.weijianzheng);
                            statusText.setText(R.string.weijianzheng);
                        }else if (status != null && status.equals("1")){
                            statusImage.setImageResource(R.drawable.jianzhengchenggong);
                            statusText.setText(R.string.jianzhengchenggong);
                        }
                        try {
                            String nick_s = URLDecoder.decode(nick,"utf-8");
                            nickText.setText(nick_s);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        if (time != null) {
                            timeText.setText(time);
                        }
                        holder.jianzhengStatusList.addView(view);
                    }
                }
            }
        }
    }

    protected class ViewHolder {
        private TextView cishu;
        private LinearLayout jianzhengStatusList;

        public ViewHolder(View view) {
            cishu = (TextView) view.findViewById(R.id.cishu);
            jianzhengStatusList = (LinearLayout) view.findViewById(R.id.jianzheng_status_list);
        }
    }
}
